using System.Collections.Generic;
using UnityEngine;
using SimpleJson;
using UnityEngine.AddressableAssets;

namespace Dolls
{
	public class cDataBase<T0, T1> where T1 : class, new()
	{
		protected Dictionary<long, T0> mDict = new Dictionary<long, T0>();

		static T1 inst = new T1();

		public static T1 Inst
		{
			get => inst;
		}

		public void Init()
		{
			Addressables.LoadAssetAsync<TextAsset>(GetPath()).Completed += (hal) =>
			{
				mDict.Clear();
				SimpleJson.JsonObject data = (SimpleJson.JsonObject)SimpleJson.SimpleJson.DeserializeObject(hal.Result.text);
				JsonArray             arr  = (JsonArray)data["its"];
				ReadData(arr);
				Addressables.Release(hal);
			};
		}

		protected virtual string GetPath()
		{
			return "";
		}

		protected virtual void ReadData(JsonArray array)
		{
		}

		public T0 FindItem(long Index)
		{
			mDict.TryGetValue(Index, out T0 info);
			return info;
		}
	}
}